import {
  checkOrderFee,
  feeSaveOrder,
  getBeforeUpdateWorkInfo,
  getOrderEndStatus,
  getTemplateByModuleAndOrderId,
  getWaitCheckFeeByOrderId,
  getWaitCheckOrderEvent,
  OrderFeeDetailProjectInfo,
  orderFeeMultipleCheck,
  saveWorkInfo,
  workFeeProjectPageSelect,
} from '@/api/order'
import { ref } from 'vue'
import _ from 'lodash'

/**
 * 服务费用申请
 */
export const useFeeCreate = () => {
  // 申请加载
  const createLoading = ref(false)

  // 申请表单
  const createForm = ref<OrderFeeAPIRequest.OrderDetailFeeForm>({
    delFeeDetailIds: [],
    feeDetails: [],
    id: '',
    remark: '',
    workId: '',
  })

  /**
   * 申请
   */
  const create = async () => {
    createLoading.value = true
    const res = await feeSaveOrder(createForm.value)
    createLoading.value = false
    return res.data
  }

  return {
    createForm,
    createLoading,
    create,
  }
}

export const useUpdateOrderInfo = () => {
  // 保存加载
  const loading = ref(false)

  // 表单
  const form = ref<OrderAPIRequest.OrderUpdateInfoForm>({
    content: '',
    delImageIds: [],
    empId: '',
    imageVos: [],
    moduleId: '',
    oneTypeId: '',
    oneWayId: '',
    threeTypeId: '',
    threeWayId: '',
    twoTypeId: '',
    twoWayId: '',
    workFieldValVos: [],
    workFieldId: '',
    faultRelationVos: [],
    faultReason: [],
    faultTypeRelationVos: [],
    solution: [],
    workSourceId: '',
    workImages: [],
    suggestedRepair: '',
    actualRepair: '',
    addressee: '',
    addresseePhone: '',
    address: '',
    project: '',
  })

  /**
   * 保存
   * @param {String} orderId - 工单id
   */
  const save = async (orderId: string) => {
    loading.value = true
    const data = _.cloneDeep(form.value)
    if (data.faultReason.length && typeof data.faultReason == 'string') {
      data.faultReason = [{ id: '0', content: data.faultReason }]
    }

    if (data.solution.length && typeof data.solution == 'string') {
      data.solution = [{ id: '0', content: data.solution }]
    }
    const res = await saveWorkInfo(data, orderId)
    loading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      content: '',
      delImageIds: [],
      empId: '',
      imageVos: [],
      moduleId: '',
      oneTypeId: '',
      oneWayId: '',
      threeTypeId: '',
      threeWayId: '',
      twoTypeId: '',
      twoWayId: '',
      workFieldValVos: [],
      workFieldId: '',
      faultRelationVos: [],
      faultReason: [],
      faultTypeRelationVos: [],
      solution: [],
      workSourceId: '',
      workImages: [],
      suggestedRepair: '',
      actualRepair: '',
      addressee: '',
      addresseePhone: '',
      address: '',
      project: '',
    }
  }

  return {
    form,
    loading,
    save,
    resetForm,
  }
}

export const useGetOrderInfoBeforeEdit = () => {
  // 获取编辑前工单信息加载
  const loading = ref(false)

  // 编辑之前的工单信息
  const info = ref<OrderInfoBeforeEdit>({
    content: '',
    delImageIds: [],
    empId: '',
    imageVos: [],
    moduleId: '',
    oneTypeId: '',
    oneWayId: '',
    source: 1,
    threeTypeId: '',
    threeWayId: '',
    twoTypeId: '',
    twoWayId: '',
    workFieldValVos: [],
    faultRelationVos: [],
    faultReason: [],
    faultTypeRelationVos: [],
    solution: [],
    workSourceId: '',
    workImages: [],
    actualRepair: '',
    suggestedRepair: '',
    address: null,
    addresseePhone: null,
    addressee: null,
    project: null,
  })

  /**
   * 获取表单
   * @param {String} orderId - 工单id
   */
  const getForm = async (orderId: string) => {
    loading.value = true
    const res = await getBeforeUpdateWorkInfo(orderId)
    loading.value = false
    info.value = res.data.data.row
    if (info.value.faultReason.length && info.value.faultReason[0].id == '0') {
      info.value.faultReason = info.value.faultReason[0].content
    }
    if (info.value.solution.length && info.value.solution[0].id == '0') {
      info.value.solution = info.value.solution[0].content
    }
  }

  return {
    info,
    loading,
    getForm,
  }
}

/**
 * 根据工单id获取工单费用
 */
export const useGetFeeByOrderId = () => {
  // 获取费用加载
  const getFeeLoading = ref(false)

  // 费用列表
  const feeList = ref<Array<OrderFeeByOrderId>>([])

  /**
   * 获取费用
   * @param id - 工单id
   */
  const getFee = async (id: string) => {
    getFeeLoading.value = true
    const res = await getWaitCheckFeeByOrderId(id)
    feeList.value = res.data.data.rows
    getFeeLoading.value = false
  }

  return {
    feeList,
    getFeeLoading,
    getFee,
  }
}

/**
 * 工单费用批量审核
 */
export const useFeeMultipleCheck = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref<OrderAPIRequest.FeeMultipleCheck>({
    checkVoList: [],
  })

  /**
   * 审核事件
   */
  const check = async () => {
    checkLoading.value = true
    const res = await orderFeeMultipleCheck(checkForm.value)
    checkLoading.value = false
    return res.data
  }

  return {
    checkForm,
    checkLoading,
    check,
  }
}

/**
 * 服务费用表单
 */
export const useFeeForm = () => {
  // 加载
  const formSaveLoading = ref(false)

  // 表单
  const form = ref<OrderAPIRequest.FeeCreateForm>({
    delFeeDetailIds: [],
    feeDetails: [],
    id: '',
    remark: '',
    workId: '',
  })

  /**
   * 申请
   */
  const formSave = async () => {
    formSaveLoading.value = true
    const res = await feeSaveOrder(form.value)
    formSaveLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      delFeeDetailIds: [],
      feeDetails: [],
      id: '',
      remark: '',
      workId: '',
    }
  }

  return {
    form,
    formSaveLoading,
    formSave,
    resetForm,
  }
}

/**
 * 获取费用项目下拉列表
 */
export const useGetFeePorjectOptionList = () => {
  // 获取下拉列表加载
  const getOptionListLoading = ref(false)

  // 获取下拉列表请求参数
  const getOptionListParam = ref({
    name: '',
    limit: 50,
    page: 1,
  })

  // 下拉选项
  const optionList = ref<Array<OrderFeeProjectOption>>([])

  /**
   * 获取下拉列表
   */
  const getOptionList = async () => {
    getOptionListLoading.value = true
    const res = await workFeeProjectPageSelect(getOptionListParam.value)
    optionList.value = res.data.data.rows
    getOptionListLoading.value = false
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}

/**
 * 费用审核
 */
export const useFeeCheck = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref<OrderAPIRequest.FeeCreateForm>({
    id: '',
    remark: '',
    feeDetails: [],
    delFeeDetailIds: [],
    workId: '',
  })

  /**
   * 审核
   */
  const check = async () => {
    checkLoading.value = true
    const res = await checkOrderFee(checkForm.value)
    checkLoading.value = false
    return res.data
  }

  return {
    checkForm,
    checkLoading,
    check,
  }
}

/**
 * 获取费用项目列表
 */
export const useGetFeePorjectListByFeeId = () => {
  // 获取项目列表加载
  const getFeePeojectListLoading = ref(false)

  // 费用项目列表
  const feeProjectList = ref<Array<OrderFeeProjectByFeeId>>([])

  /**
   * 获取项目列表
   * @param id - 费用id
   */
  const getFeeProjectList = async (id: string) => {
    getFeePeojectListLoading.value = true
    const res = await OrderFeeDetailProjectInfo(id)
    feeProjectList.value = res.data.data.row
    getFeePeojectListLoading.value = false
  }

  return {
    feeProjectList,
    getFeePeojectListLoading,
    getFeeProjectList,
  }
}

/**
 * 获取工单待处理事件列表
 */
export const useGetOrderWaitHandleList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 列表
  const list = ref<Array<OrderWaitHandleEvent>>([])

  /**
   * 获取列表
   * @param {String} id - 工单id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getWaitCheckOrderEvent(id)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    getListLoading,
    getList,
  }
}

/**
 * 在结单界面获取工单模板：根据工单类型和工单ID获取工单模板
 */
export const useGetTemplateByModuleAndOrderId = () => {
  const getTemplateLoading = ref(false)
  const getTemplateParam = ref({
    moduleId: '',
    orderId: '',
  })
  const template = ref<OrderTemplateForUpdateEndTemplate>({
    authority: null,
    objects: [],
    custAccId: '',
    workId: '',
  })
  const getTemplate = async () => {
    getTemplateLoading.value = true
    const res = await getTemplateByModuleAndOrderId(
      getTemplateParam.value.moduleId,
      getTemplateParam.value.orderId,
    )
    getTemplateLoading.value = false
    if (res.data.code === 20000) {
      template.value = res.data.data
      return ''
    }
    return res.data.message
  }

  return {
    template,
    getTemplateParam,
    getTemplateLoading,
    getTemplate,
  }
}

/**
 * 获取工单结单状态
 */
export const useGetOrderEndStatus = () => {
  // 获取状态加载
  const getStatusLoading = ref(false)

  // 结单状态
  // true - 已申请结单
  // false - 未申请结单
  const status = ref(false)

  /**
   * 获取结单状态
   * @param {string} id - 工单id
   */
  const getStatus = async (id: string) => {
    getStatusLoading.value = true
    const res = await getOrderEndStatus(id)
    getStatusLoading.value = false
    if (res.data.code === 20000) {
      status.value = res.data.data
      return ''
    }
    return res.data.message
  }

  return {
    status,
    getStatusLoading,
    getStatus,
  }
}
